/**
 * Created by csy on 2017/11/20.
 */
var $alert = require("libs/alert");
var isSale = require('libs/user-type').isSale;
require(['jquery','libs/confirm','easyui'],function($,$confirm){
    $.parser.parse();
    tableInit();
    //查询按钮点击事件
    $("#main_search").on("click",function () {
        $('#main_table').datagrid('load',queryParams());
    });4
    $(document).on("click",'a[name=open]',function () {
        var id = $(this).data('id');
        $confirm("是否确定启用？",function () {
            ajaxRequestMethod({id: id,status:"Y"},"api/v1/p/mms/promotion/updateStatus");
        });
    });
    $(document).on("click",'a[name=close]',function () {
        var id = $(this).data('id');
        $confirm("是否确认禁用？",function () {
            ajaxRequestMethod({id: id,status:"N"},"api/v1/p/mms/promotion/updateStatus");
        });
    });
    $(document).on("click",'a[name=look]',function () {
        window.parent.tabOpen($(this).attr('ref'),"推广人管理详情",true,true);
    });
});

function tableInit() {
    $("#main_table").datagrid({
        url:'api/v1/p/mms/promotion/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        rownumbers:true,
        singleSelect: true,
        emptyMsg : "暂无数据",
        queryParams:queryParams(),
        columns:[[ {
            field : 'code',
            title : '推广代码',
            width:"12%",
            formatter: function(value,row,index){
                return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.id+"'>"+value+"</a>";
            }
        },{
            field : 'name',
            title : '姓名',
            width:"40%",
            formatter: function(value,row,index){
                return '<span  title='+value+'>'+value+'</span>'
            }
        },{
            field : 'phone',
            title : '联系电话',
            width:"8%"
        },{
            field : 'url',
            title : '推广链接',
            width:"8%"
        },{
            field:'userName',
            title:'关联用户',
            width:"8%"
        },{
            field:'createdDt',
            title:'创建时间',
            width:"12%"
        },{
            field:'nums',
            title:'注册用户数',
            width:"8%"
        },{
            field:'sts',
            title:'状态',
            width:"6%",
            formatter:function (value,row,index) {
                switch (value){
                    case "Y": return "有效";
                    case "N": return "无效";
                    default : return "";
                }
            }
        },{
            field:'opt',
            title:'操作',
            hidden:isSale,
            width:"10%",
            formatter:function (value,row,index) {
                switch (row.sts){
                    case "Y": return "<a name='modify' href='javascript:;' ref='promotion-add?ticketId="+row.id+"'>查看</a>|<a href='javascript:;' name='close' data-id='"+row.id+"'>关闭工单</a>";
                    case "N": return "<a name='look' href='javascript:;' ref='promotion-add?ticketId="+row.id+"'>查看</a>|<a href='javascript:;' name='open' data-id='"+row.id+"'>开启工单</a>";
                    default : return "";
                }
            }
        }
        ]],
        loadFilter:function(data){
            if(data.code=='200'){
                return data.data;
            }else{
                return [];
            }
        },
        toolbar:"#tb"
    });
}
function queryParams() {
    var d1 = $('#startDt').datebox('getValue');
    var d2 = $('#endDt').datebox('getValue');
    var data =
        {
            ticketNo:$('#ticketNo').val(),
            customerName:$('#customerName').val(),
            warehouseId:$('#warehouseId').combobox('getValue'),
            dateFrom:d1?$('#startDt').data('utc0'):'',
            dateTo:d2?$('#endDt').data('utc0'):''
        };
    return data;
}
function ajaxRequestMethod(data,url) {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(data),
        type:"post",
        url:url,
        success:function (result) {
            if(result.code==200){
                $('#main_table').datagrid('load');
            }else {
                $alert(result.message);
            }
        },
        error:function () {
            console.log(url+" error")
        }
    });
}
